package com.zhipu.balance.ui.vm.home.calculate.wheat

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.SPUtils
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordWheat
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils

/**
 * 小麦不完善粒
 */
class WheeatFaultinessViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){


    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 小麦实体
     */
    var mRecordWheat : RecordWheat? = null


    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null




    /**
     * 不完善粒质量
     */
    var imperfectGrainQuality = ObservableField("")


    /**
     * 虫蚀粒质量
     */
    val insectBoredQuality = ObservableField("")

    /**
     * 病斑粒质量
     */
    val mottledQuality = ObservableField("")

    /**
     * 赤霉病粒质量
     */
    var qualityOfFusariumGraminearumGrain = ObservableField("")

    /**
     * 破损粒质量
     */
    var brokenQuality = ObservableField("")

    /**
     * 生芽粒重量
     */
    var sproutWeight = ObservableField("")

    /**
     * 生霉粒重量
     */
    var moldWeight = ObservableField("")

    /**
     * 热损伤粒质量
     */
    var thermalDamageGrainQuality = ObservableField("")


    /**
     * 霉变粒质量
     */
    var mildewGrainQuality = ObservableField("")




    /**
     * 小样重量
     */
    var smallMiscellaneousSampleQuality =  ObservableField("")

    /**
     * 大样杂质率
     */
    var detailResults =  ObservableField("")





    /**
     * 虫蚀率-结果
     */
    var insectBoredResult = ObservableField("")
    /**
     * 病斑率-结果
     */
    var mottledResult = ObservableField("")
    /**
     * 赤霉病粒-结果
     */
    var scabGrainResults = ObservableField("")
    /**
     * 破损率-结果
     */
    var brokenResult = ObservableField("")
    /**
     * 生芽粒率-结果
     */
    var sproutResult = ObservableField("")
    /**
     * 生霉粒率-结果
     */
    var moldResult = ObservableField("")
    /**
     * 热损伤粒 结果
     */
    var thermalDamageGrain = ObservableField("")
    /**
     * 霉变粒结果
     */
    var mildewGrain = ObservableField("")




    /**
     * 不完善粒-结果
     */
    var resultOfImperfectGrain = ObservableField("")


    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordWheat?.imperfectGrainRecorder)) {
            //虫蚀粒质量
            insectBoredQuality.set(mRecordWheat?.insectBoredQuality.toString())
            //病斑粒质量
            mottledQuality.set(mRecordWheat?.mottledQuality.toString())
            //热损伤粒质量
            qualityOfFusariumGraminearumGrain.set(mRecordWheat?.qualityOfFusariumGraminearumGrain.toString())
            //破损粒质量
            brokenQuality.set(mRecordWheat?.brokenQuality.toString())
            //生芽粒重量
            sproutWeight.set(mRecordWheat?.sproutWeight.toString())
            //生霉粒重量
            moldWeight.set(mRecordWheat?.moldWeight.toString())
            //热损伤粒质量
            thermalDamageGrainQuality.set(mRecordWheat?.thermalDamageGrainQuality.toString())
            //霉变粒质量
            mildewGrainQuality.set(mRecordWheat?.mildewGrainQuality.toString())



            //小样重量
            smallMiscellaneousSampleQuality.set(mRecordWheat?.smallMiscellaneousSampleQuality.toString())
            //大样杂质率
            detailResults.set(mRecordWheat?.detailResults.toString())



            //虫蚀率-结果
            insectBoredResult.set(mRecordWheat?.insectBoredResult.toString())
            //病斑率-结果
            mottledResult.set(mRecordWheat?.mottledResult.toString())
            //热损伤粒 结果
            scabGrainResults.set(mRecordWheat?.scabGrainResults.toString())
            //破损率-结果
            brokenResult.set(mRecordWheat?.brokenResult.toString())
            //生芽粒率-结果
            sproutResult.set(mRecordWheat?.sproutResult.toString())
            //生霉粒率-结果
            moldResult.set(mRecordWheat?.moldResult.toString())
            //热损伤粒 结果
            thermalDamageGrain.set(mRecordWheat?.thermalDamageGrain.toString())
            //霉变粒结果
            mildewGrain.set(mRecordWheat?.mildewGrain.toString())
            //不完善粒-结果
            resultOfImperfectGrain.set(mRecordWheat?.resultOfImperfectGrain.toString())
        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //虫蚀粒质量
        insectBoredQuality.set("")
        //病斑粒质量
        mottledQuality.set("")
        //热损伤粒质量
        qualityOfFusariumGraminearumGrain.set("")
        //破损粒质量
        brokenQuality.set("")
        //生芽粒重量
        sproutWeight.set("")
        //生霉粒重量
        moldWeight.set("")
        //热损伤粒质量
        thermalDamageGrainQuality.set("")
        //霉变粒质量
        mildewGrainQuality.set("")



        //小样重量
        smallMiscellaneousSampleQuality.set("")
        //大样杂质率
        detailResults.set("")


        //虫蚀率-结果
        insectBoredResult.set("")
        //病斑率-结果
        mottledResult.set("")
        //热损伤粒 结果
        scabGrainResults.set("")
        //破损率-结果
        brokenResult.set("")
        //生芽粒率-结果
        sproutResult.set("")
        //生霉粒率-结果
        moldResult.set("")
        //热损伤粒 结果
        thermalDamageGrain.set("")
        //霉变粒结果
        mildewGrain.set("")
        //不完善粒-结果
        resultOfImperfectGrain.set("")
    }


    /**
     * 不完善粒质量
     */
    fun imperfectGrainQuality(){
        imperfectGrainQuality.set(weighing)
        mRecordWheat?.imperfectGrainQuality = imperfectGrainQuality.get()?.toDouble()
    }


    /**
     * 虫蚀粒质量
     */
    fun insectBoredQuality(){
        insectBoredQuality.set(weighing)
        mRecordWheat?.insectBoredQuality = insectBoredQuality.get()?.toDouble()
    }


    /**
     * 病斑粒质量
     */
    fun mottledQuality(){
        mottledQuality.set(weighing)
        mRecordWheat?.mottledQuality = mottledQuality.get()?.toDouble()
    }

    /**
     * 赤霉病粒质量
     */
    fun qualityOfFusariumGraminearumGrain(){
        qualityOfFusariumGraminearumGrain.set(weighing)
        mRecordWheat?.qualityOfFusariumGraminearumGrain = qualityOfFusariumGraminearumGrain.get()?.toDouble()
    }


    /**
     * 破损粒质量
     */
    fun brokenQuality(){
        brokenQuality.set(weighing)
        mRecordWheat?.brokenQuality = brokenQuality.get()?.toDouble()
    }

    /**
     * 生芽粒重量
     */
    fun sproutWeight(){
        sproutWeight.set(weighing)
        mRecordWheat?.sproutWeight = sproutWeight.get()?.toDouble()
    }


    /**
     * 生霉粒重量
     */
    fun moldWeight(){
        moldWeight.set(weighing)
        mRecordWheat?.moldWeight = moldWeight.get()?.toDouble()
    }

    /**
     * 热损伤粒质量
     */
    fun thermalDamageGrainQuality(){
        thermalDamageGrainQuality.set(weighing)
        mRecordWheat?.thermalDamageGrainQuality = thermalDamageGrainQuality.get()?.toDouble()
    }


    /**
     * 霉变粒质量
     */
    fun mildewGrainQuality(){
        mildewGrainQuality.set(weighing)
        mRecordWheat?.mildewGrainQuality = mildewGrainQuality.get()?.toDouble()
    }





    /**
     * 刷新数据
     */
    fun refreshData(){
        //小样重量
        smallMiscellaneousSampleQuality.set(mRecordWheat?.smallMiscellaneousSampleQuality.toString())
        //大样杂质率
        detailResults.set(mRecordWheat?.detailResults.toString())
    }


    /**
     * 计算
     */
    fun calculation(){
        /**
         * 计算大样
         */
        var details = 100 - mRecordWheat?.detailResults!!


        /**
         * 是否细分
         */
        if(SPUtils.getInstance().getBoolean("ImperfectSubdivision",false)){

            /**
             * 虫蚀率-结果
             */
            var InsectBoredResult = CommonUtils.formatDouble(details * (mRecordWheat?.insectBoredQuality!! / mRecordWheat?.smallMiscellaneousSampleQuality!!))
            mRecordWheat?.insectBoredResult = InsectBoredResult
            insectBoredResult.set(InsectBoredResult.toString())
            /**
             * 病斑率-结果
             */
            var MottledResult = CommonUtils.formatDouble(details * (mRecordWheat?.mottledQuality!! / mRecordWheat?.smallMiscellaneousSampleQuality!!))
            mRecordWheat?.mottledResult = MottledResult
            mottledResult.set(MottledResult.toString())
            /**
             * 赤霉病粒-结果
             */
            var ScabGrainResults = CommonUtils.formatDouble(details * (mRecordWheat?.qualityOfFusariumGraminearumGrain!! / mRecordWheat?.smallMiscellaneousSampleQuality!!))
            mRecordWheat?.scabGrainResults = ScabGrainResults
            scabGrainResults.set(ScabGrainResults.toString())
            /**
             * 破损率-结果
             */
            var BrokenResult = CommonUtils.formatDouble(details * (mRecordWheat?.brokenQuality!! / mRecordWheat?.smallMiscellaneousSampleQuality!!))
            mRecordWheat?.brokenResult = BrokenResult
            brokenResult.set(BrokenResult.toString())
            /**
             * 生芽粒率-结果
             */
            var SproutResult = CommonUtils.formatDouble(details * (mRecordWheat?.sproutWeight!! / mRecordWheat?.smallMiscellaneousSampleQuality!!))
            mRecordWheat?.sproutResult = SproutResult
            sproutResult.set(SproutResult.toString())
            /**
             * 生霉粒率-结果
             */
            var MoldResult = CommonUtils.formatDouble(details * (mRecordWheat?.moldWeight!! / mRecordWheat?.smallMiscellaneousSampleQuality!!))
            mRecordWheat?.moldResult = MoldResult
            moldResult.set(MoldResult.toString())

            /**
             * 热损伤粒 结果
             */
            var mThermalDamageGrain = CommonUtils.formatDouble(details * (mRecordWheat?.thermalDamageGrainQuality!! / mRecordWheat?.smallMiscellaneousSampleQuality!!))
            mRecordWheat?.thermalDamageGrain = mThermalDamageGrain
            thermalDamageGrain.set(mThermalDamageGrain.toString())

            /**
             * 霉变粒结果
             */
            var MildewGrain = CommonUtils.formatDouble((details * (mRecordWheat?.mildewGrainQuality !! / mRecordWheat?.smallMiscellaneousSampleQuality!!)))
            mRecordWheat?.mildewGrain = MildewGrain
            mildewGrain.set(MildewGrain.toString())


            /**
             * 不完善粒-结果
             */
            var ImperfectGrain = CommonUtils.formatDouble(InsectBoredResult + MottledResult + ScabGrainResults + BrokenResult + SproutResult + MoldResult + mThermalDamageGrain + MildewGrain)
            mRecordWheat?.resultOfImperfectGrain = ImperfectGrain
            resultOfImperfectGrain.set(ImperfectGrain.toString())

        }else{
            /**
             * 计算小样
             */
            var smallKind = mRecordWheat!!.imperfectGrainQuality / mRecordWheat!!.smallMiscellaneousSampleQuality
            /**
             * 不完善粒-结果
             */
            var ImperfectGrain = CommonUtils.formatDouble(details * smallKind)
            mRecordWheat?.resultOfImperfectGrain = ImperfectGrain
            resultOfImperfectGrain.set(ImperfectGrain.toString())

        }

        /**
         * 不完善粒记录员
         */
        mRecordWheat?.imperfectGrainRecorder = mPersonnel?.name

        //虫蚀粒质量
        if(mRecordWheat?.insectBoredQuality == 0.00){
            insectBoredQuality.set("0.00")
        }
        //病斑粒质量
        if(mRecordWheat?.mottledQuality == 0.00){
            mottledQuality.set("0.00")
        }
        //赤霉病粒质量
        if(mRecordWheat?.qualityOfFusariumGraminearumGrain == 0.00){
            qualityOfFusariumGraminearumGrain.set("0.00")
        }
        //破损粒质量
        if(mRecordWheat?.brokenQuality == 0.00){
            brokenQuality.set("0.00")
        }
        //生芽粒重量
        if(mRecordWheat?.sproutWeight == 0.00){
            sproutWeight.set("0.00")
        }
        //生霉粒重量
        if(mRecordWheat?.moldWeight == 0.00){
            moldWeight.set("0.00")
        }
        //热损伤粒质量
        if(mRecordWheat?.thermalDamageGrainQuality == 0.00){
            thermalDamageGrainQuality.set("0.00")
        }
        //霉变粒质量
        if(mRecordWheat?.mildewGrainQuality == 0.00){
            mildewGrainQuality.set("0.00")
        }

        //不完善粒质量
        if(mRecordWheat?.imperfectGrainQuality == 0.00){
            imperfectGrainQuality.set("0.00")
        }
    }
}